package org.example.myleet.learn.web;

import org.example.myleet.learn.StopWatch;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Arrays;
import java.util.Scanner;

public class SocketTest {
    public static void main(String[] args) throws IOException {
        StopWatch stopWatch = new StopWatch();
        stopWatch.printTimeElapse("Start");
        Socket socket = new Socket();
        stopWatch.printTimeElapse("Socket created");
        InetAddress[] addresses = InetAddress.getAllByName("time-a.nist.gov");
        int[][] addressArr = new int[addresses.length][];
        for (int i = 0; i < addresses.length; ++i) {
            byte[] a = addresses[i].getAddress();
            int[] address = new int[a.length];
            for (int j = 0; j < a.length; ++j) {
                if (a[j] < 0) {
                    address[j] = ((int) Byte.MAX_VALUE + 1) * 2 + (int) a[j];
                } else {
                    address[j] = a[j];
                }
            }
            addressArr[i] = address;
        }
        System.out.println(Arrays.deepToString(addressArr));
        stopWatch.printTimeElapse("Address parsed");
        socket.connect(new InetSocketAddress("time-a.nist.gov", 13), 10000);
        stopWatch.printTimeElapse("Socket connected");
        Scanner in = new Scanner(socket.getInputStream(), "UTF-8");
        stopWatch.printTimeElapse("Scanner created");
        while (in.hasNext()) {
            System.out.println(in.nextLine());
        }
        stopWatch.printTimeElapse("Finish");
    }
}
